﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web.SystemLogin.Services;
using Web.SystemLogin.Models;
using DreamBuilder.Core.Message;

namespace Web.SystemLogin.Business
{
    public class OnlineUser
    {

        private static OnlineUserDs.OnlineUserDataTable alluser;

        public OnlineUserDs.OnlineUserDataTable AllUser
        {
            get { return alluser; }
        }

        public OnlineUser()
        {
            alluser = new OnlineUserDs.OnlineUserDataTable();
        }
        /// <summary>
        /// 功能说明：将当前用户加入在线列表
        /// 如果该用户的数据当前仍然在在线列表中，则更新
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool AddUserToOnLine(User user)
        {

            //开始搜索是否已经存在该用户,如果存在则是改变数据,否则添加新的用户
            string strExpr;
            strExpr = "SessionID = '" + user.SessionID + "'";

            OnlineUserDs.OnlineUserRow[] curUser;

            // Use the Select method to find all rows matching the filter.

            curUser = (OnlineUserDs.OnlineUserRow[])alluser.Select(strExpr);

            if (curUser.Length > 0)
            {
                for (int i = 0; i < curUser.Length; i++)
                {
                    curUser[i].User_ID = user.User_ID;
                    curUser[i].User_Name = user.User_Name;
                    curUser[i].Real_ID = user.Real_ID;
                    curUser[i].Login_ID = user.Login_ID;
                    curUser[i].Last_Login_Time = user.Last_Login_Time;
                    curUser[i].This_Login_Time = user.This_Login_Time;
                    curUser[i].ORG_ID= user.ORG_ID;
                }
            }
            else
            {
                //直接加入新的数据
                OnlineUserDs.OnlineUserRow myRow;
                try
                {
                    myRow = alluser.NewOnlineUserRow();
                    // Then add the new row to the collection.
                    myRow.User_ID = user.User_ID;
                    myRow.Real_ID = user.Real_ID;
                    myRow.User_Name = user.User_Name;
                    myRow.Login_ID = user.Login_ID;
                    myRow.Last_Login_Time = user.Last_Login_Time;
                    myRow.This_Login_Time = user.This_Login_Time;
                    myRow.ORG_ID = user.ORG_ID;
                    alluser.Rows.Add(myRow);
                }
                catch (Exception e)
                {
                    throw (new Exception(e + "--------------------" + e.Message));
                }
            }
            alluser.AcceptChanges();
            return true;
        }

        /// <summary>
        /// 功能说明:判断某用户是否在线,本部分暂时不用
        /// </summary>
        /// <param name="name"></param>
        /// <returns>TRUE代表在线，FALSE不在</returns>
        public bool IsUserOnLine(string name)
        {
            //需要先判断用户是否已经在用户列表中了
            //开始搜索是否已经存在该用户,如果存在则是改变数据,否则添加新的用户
            string strExpr;
            strExpr = "UserName ='" + name + "'";
            OnlineUserDs.OnlineUserRow[] curUser;
            // Use the Select method to find all rows matching the filter.
            curUser = (OnlineUserDs.OnlineUserRow[])alluser.Select(strExpr);
            if (curUser.Length > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DelelteOnline(string sessionID)
        {
            //开始搜索是否已经存在该用户,如果存在则是改变数据,否则添加新的用户
            string strExpr;
            strExpr = "SessionID = '" + sessionID + "'";
            OnlineUserDs.OnlineUserRow[] curUser;

            // Use the Select method to find all rows matching the filter.

            curUser = (OnlineUserDs.OnlineUserRow[])alluser.Select(strExpr);

            if (curUser.Length > 0)
            {
                //删除这些记录
                for (int i = 0; i < curUser.Length; i++)
                {
                    curUser[i].Delete();
                }
                alluser.AcceptChanges();
            }
            return true;
        }

        /// <summary>
        /// 判断用户是否在线
        /// </summary>
        /// <param name="sessionID"></param>
        /// <param name="User_ID"></param>
        /// <param name="Real_ID"></param>
        /// <param name="User_Name"></param>
        /// <param name="Login_ID"></param>
        /// <param name="Last_Login_Time"></param>
        /// <param name="This_Login_Time"></param>
        /// <param name="ORG_ID"></param>
        /// <returns></returns>
        public bool CheckUserOnLine(string sessionID, string User_ID,string Real_ID, string User_Name, string Login_ID, DateTime Last_Login_Time, DateTime This_Login_Time, string ORG_ID)
        {
            //需要先判断用户是否已经在用户列表中了
            User newuser = new User();
            newuser.SessionID = sessionID;
            newuser.User_ID = User_ID;
            newuser.Real_ID = Real_ID;
            newuser.User_Name = User_Name;
            newuser.Login_ID = Login_ID;
            newuser.Last_Login_Time = Last_Login_Time;
            newuser.This_Login_Time = This_Login_Time;
            newuser.ORG_ID = ORG_ID;
            this.AddUserToOnLine(newuser);
            return true;
        }

        public string GetLogInfo(string User_ID,string User_Name, string UserHostAddress, string platform, string MacAddress, string Browser)
        {
            StringBuilder logInfo = new StringBuilder();
            logInfo.Append(FormatValueStr(User_ID, 64));
            logInfo.Append(FormatValueStr(User_Name,64));
            logInfo.Append(FormatValueStr(UserHostAddress, 64));
            logInfo.Append(FormatValueStr(platform, 64));
            logInfo.Append(FormatValueStr(MacAddress, 64));
            logInfo.Append(FormatValueStr(Browser, 64));
            return logInfo.ToString();
        }

        /// <summary>
        /// 复制string的值到字符串中,并补齐空格,返回字符串
        /// </summary>
        /// <param name="value"></param>
        /// <param name="array"></param>
        /// <returns></returns>
        private string FormatValueStr(string value, int length)
        {
            char[] array = new char[length];
            //将传进来的类型转换成字符串类型
            char[] arrValue = value.ToCharArray();
            //如果传进来的字符串的长度大于规定的长度
            if (arrValue.Length > array.Length)
            {
                throw MessageHandler.CreateMessageByMessage("记录日志时值的长度超过规定的长度!");
            }
            //赋值
            int i = 0;
            for (; i < arrValue.Length; i++)
            {
                array[i] = arrValue[i];
            }
            //补空格
            for (; i < array.Length; i++)
            {
                array[i] = ' ';
            }
            string result = new string(array);
            return result;
        }
    }
}
